package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.xueyu.dao.UserMapper;
import com.xueyu.dao.rpDao;
import com.xueyu.domain.CacheManager;
import com.xueyu.domain.UserVo;
import com.xueyu.domain.rp;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@CrossOrigin
@Controller
@RequestMapping(value = "/11011")
@ContextConfiguration(locations = {"classpath:mybatis.xml"})
public class Controller1104 {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private com.xueyu.dao.rpDao rpDao;

    @RequestMapping(value = "/login")
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String role = request.getParameter("role");
        String remember = request.getParameter("remember");
        String autoONotify = request.getParameter("autoONotify");
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<UserVo> queryWrapper = Wrappers.query();
        queryWrapper.eq("userName", userName)  // 添加条件：名字等于指定的值
                .eq("passWord", passWord);     // 添加条件：密码等于指定的值
        List<UserVo> userList = userMapper.selectList(queryWrapper);
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        session.setAttribute("userVo", role);
        application.setAttribute("operator",userList);

        if (userList.isEmpty()) {
            map.put("code", 500);
            map.put("msg", "请检查用户名和密码和岗位是否正确!!");
        } else {
            map.put("code", 200);
            map.put("msg", "登录成功!!");
            QueryWrapper<rp> queryWrapper2 = new QueryWrapper<rp>();
            queryWrapper2.select("SPID"); // 指定要选择的字段
            for (UserVo user : userList) {
                map.put("pic",user.getDrugPic());
                map.put("role",user.getRole());
                map.put("time",user.getTime());
                queryWrapper2.eq("RoleID",user.getRole());
            }
            List<rp> SPIDlist= rpDao.selectList(queryWrapper2);
            List<Integer> spidList = new ArrayList<>();
            for (rp rpItem : SPIDlist) {
                spidList.add(rpItem.getSPID());
            }
            map.put("list",spidList);
            CacheManager.put("myData", role);
            Date date = new Date();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = sdf1.format(date);
            UserVo user = new UserVo();
            user.setTime(currentTime);
            UpdateWrapper<UserVo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userName", userName);
            updateWrapper.eq("passWord", passWord);
            userMapper.update(user, updateWrapper);
        }
        String data = (String) CacheManager.get("myData");
        CacheManager.put("userList", userList);
        System.out.println(data);
        return map;
    }
}

















